package com.weasel.im.server.initializer;

import com.weasel.im.common.manager.IMSessionManager;
import com.weasel.im.common.manager.LeaveMessageCacheManager;
import com.weasel.im.common.manager.UserSessionRelationManager;
import com.weasel.im.server.handler.MessageHandler;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by Dylan on 2015/12/3.
 */
public abstract class AbstractMessageHandlerInitializer {

    protected MessageHandlerHolder handlerHolder = new MessageHandlerHolder();

    protected IMSessionManager sessionManager;
    protected UserSessionRelationManager relationManager;

    protected LeaveMessageCacheManager leaveMessageCacheManager;

    public AbstractMessageHandlerInitializer(IMSessionManager sessionManager, UserSessionRelationManager relationManager, LeaveMessageCacheManager leaveMessageCacheManager) {
        this.sessionManager = sessionManager;
        this.relationManager = relationManager;
        this.leaveMessageCacheManager = leaveMessageCacheManager;
        initHander(handlerHolder);
    }

    protected abstract void initHander(MessageHandlerHolder handlerHolder);

    public List<MessageHandler> getHandlers() {
        return handlerHolder.getHandlers();
    }

    static class MessageHandlerHolder{

        private List<MessageHandler> handlers = new LinkedList<MessageHandler>();


        public List<MessageHandler> getHandlers() {
            return handlers;
        }

        public void setHandlers(List<MessageHandler> handlers) {
            this.handlers = handlers;
        }

        public MessageHandlerHolder addLast(MessageHandler messageHandler){

            handlers.add(messageHandler);
            return this;
        }
    }
}


